<?php
include_once("survey_answer_user_dao_base.php");

class survey_answer_user_dao extends survey_answer_user_dao_base
{
	function get_number_by_survey_question_id($survey_question_id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("
SELECT Count(1) 
FROM survey_answer sa
     INNER JOIN survey_question sq ON sq.survey_question_id = sa.survey_question_id AND sa.survey_question_id = :survey_question_id
     INNER JOIN survey_answer_user sau ON sau.survey_answer_id = sa.survey_answer_id");
			$stmt->bindParam(':survey_question_id', $survey_question_id, PDO::PARAM_INT);
			$stmt->execute();
			$row = $stmt->fetch();
			
			
			$dbh->commit();
			$dbh = null;
			
			return $row[0];
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_number_by_survey_answer_id($survey_answer_id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("
SELECT Count(1) 
FROM survey_answer sa
     INNER JOIN survey_question sq ON sq.survey_question_id = sa.survey_question_id
     INNER JOIN survey_answer_user sau ON sau.survey_answer_id = sa.survey_answer_id AND sa.survey_answer_id = :survey_answer_id");
			$stmt->bindParam(':survey_answer_id', $survey_answer_id, PDO::PARAM_INT);
			$stmt->execute();
			$row = $stmt->fetch();
			
			
			$dbh->commit();
			$dbh = null;
			
			return $row[0];
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_survey_answer_user_by_survey_answer_id($survey_answer_id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$aos = array();
			$stmt = $dbh->query("SELECT * FROM survey_answer_user s WHERE s.survey_answer_id = ".$survey_answer_id);
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$s = new survey_answer_user($row["survey_answer_user_id"],$row["survey_answer_id"],$row["user_id"],$row["created_date"]);
				$aos[] = $s;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aos;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
}
?>